<template>
  <div>
    <el-select v-if="prompts.length" v-model="prompt_id" size="mini" placeholder="请选择PROMPT">
      <el-option
        v-for="item in prompts"
        :key="item.id"
        :label="item.name"
        :value="item.id"
      />
    </el-select>
    <el-button v-if="prompts.length" :loading="aiLoading" type="text" @click="handleAiReplay">执行</el-button>
  </div>
</template>

<script>
import { AIReplyEmail, AIGetPrompts } from '@/api/crm/crmai'
export default {
  name: 'Aireply',
  props: ['crmcustomer_id', 'mailbox_id', 'chat_id', 'type'],
  data() {
    return {
      prompt_id: '',
      prompts: [],
      aiLoading: false
    }
  },
  // type:email_prompt,quote_summary
  watch: {
    type: {
      immediate: true,
      handler(newType) {
        if (newType) {
          this.handleGetAIPrompts()
        }
      }
    }
  },
  methods: {
    handleAiReplay() {
      this.aiLoading = true
      AIReplyEmail({ prompt_id: this.prompt_id, mailbox_id: this.mailbox_id, chat_id: this.chat_id, crmcustomer_id: this.crmcustomer_id }).then(response => {
        this.aiLoading = false
        this.$emit('success', response.data)
      }).catch(err => {
        this.aiLoading = false
        console.log(err)
      })
    },
    handleGetAIPrompts() {
      AIGetPrompts({ t: this.type }).then(response => {
        this.prompts = response.data
        // 赋值默认第一个prompt_id
        if (this.prompts.length) {
          this.prompt_id = this.prompts[0].id
        }
      }).catch(err => {
        console.log(err)
      })
    }
  }
}
</script>

<style scoped>

</style>
